/*
 * @author Jordi Mateo
 * @author Sergi Builtrago
 * @author Marc Gonzalez
 * @author Aitor Garcia
 * @version 23.12.2012
 */

package com.example.project;

import java.lang.ref.WeakReference;
import com.example.service.ComerParcelable;
import com.example.service.ValoracionsParcelable;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class GridItemClicker  implements OnItemClickListener{

	Messenger mMessenger; 
	private Intent c;
	@Override
	public void onItemClick(AdapterView<?> adapter, View v, int position, long l) {
		switch(position){
		case 0:
        	c = new Intent(v.getContext(), Ciutat.class);
        	v.getContext().startActivity(c);
        break;
	 
        case 1:
        	mMessenger = new Messenger(new IncomingHandler(null,v));
        	Bundle b = new Bundle();
    		Message msg = Message.obtain(null, DateBaseService.MSG_GET_ALL_RESTAURANTS, 0, 0);
    		msg.setData(b);
    		msg.replyTo=mMessenger;
    		try {
    			MainActivity.mService.send(msg);
    		} catch (RemoteException e) {
    			e.printStackTrace();
    		}
    		Bundle b2 = new Bundle();
    		Message msg2 = Message.obtain(null, DateBaseService.MSG_GET_ALL_VALORACIONES, 0, 0);
    		msg2.setData(b2);
    		msg2.replyTo=mMessenger;
    		try {
    			MainActivity.mService.send(msg2);
    		} catch (RemoteException e) {
    			e.printStackTrace();
    		}
        	
        break;
        case 2:
        	c= new Intent(v.getContext(),Dormir.class);
       	 	v.getContext().startActivity(c);
        break;
        case 3:
        	c = new Intent(v.getContext(), ProxAlertActivity.class);
        	v.getContext().startActivity(c);
        	break;
        case 4:
        	Toast.makeText(v.getContext(),R.string.not_implemented, Toast.LENGTH_SHORT).show();
        	break;
        case 5:
        	Toast.makeText(v.getContext(),R.string.not_implemented, Toast.LENGTH_SHORT).show();
        	break;
        	
       
		}
	}
	
static class IncomingHandler extends Handler {
		
		WeakReference<Menjar> mService;
		private Intent c;
		View v; 
		IncomingHandler(Menjar aMenjar, View v) {
            mService = new WeakReference<Menjar>(aMenjar);
            this.v=v;
        }

    	@Override
    	public void handleMessage(Message msg) {
           
    		switch (msg.what) {
    			case Delete_Restaurant_DB.MSG_OBTAIN_ALL_RESTAURANTS: 	
    				Menjar.restaurantsList.clear();
    				Bundle b = msg.getData();
    				b.setClassLoader(ComerParcelable.class.getClassLoader());
    				ComerParcelable cp  =  (ComerParcelable) b.getParcelable("RestaurantParcelable");
    				Menjar.restaurantsList= cp.getRestaurantsList();
    				c= new Intent(v.getContext(),Menjar.class);
    	        	v.getContext().startActivity(c);
    	        	break;
    	        	
    			case Menjar.MSG_OBTAIN_ALL_VALORACIONS: 	
    				Menjar.valoracionsList.clear();
    				Bundle b1 = msg.getData();
    				b1.setClassLoader(ValoracionsParcelable.class.getClassLoader());
    				ValoracionsParcelable cp1  =  (ValoracionsParcelable) b1.getParcelable("ValoracionParcelable");
    				Menjar.valoracionsList= cp1.getRestaurantsList();
    				c= new Intent(v.getContext(),Menjar.class);
    	        	v.getContext().startActivity(c);
    	        	break;
    	}
    	}
	}
}
